package org.eclipse.wst.server.preview.openejb.jetty.loader;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;

import org.apache.openejb.OpenEJB;
import org.apache.openejb.core.ServerFederation;
import org.apache.openejb.loader.Loader;
import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.server.ejbd.EjbServer;
import org.eclipse.wst.server.preview.openejb.jetty.builder.EJBInjectionManager;
import org.eclipse.wst.server.preview.openejb.jetty.builder.OpenejbWebappBinder;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;

/**OpenEJBLoader : This class has just one public method, 
 * the method public void init() . This method initiates 
 * and starts the OpenEJB http server. After that, every 
 * web app deployed by the Jetty web server is searched 
 * for a EJB modules. The searching process is made calling 
 * the start( WebAppContext webapp ) method of the class 
 * OpenejbWebappBinder. 
 * 
 * @author Marcelo de Oliveira Dias
 *
 */

public class OpenEJBLoader 	implements Loader {
	
	private OpenejbWebappBinder openejbBinder;
	
	public void init( Properties p ){
		try{
			
	        SystemInstance.init( p );
			
	        EjbServer ejbServer = new EjbServer();
	        SystemInstance.get().setComponent(EjbServer.class, ejbServer);
	        OpenEJB.init(p, new ServerFederation());
	        ejbServer.init( p );
	        ejbServer.start();
	       
	        openejbBinder = new OpenejbWebappBinder( );
	        
	        bindRunningApplications();
	        
	        injectEJBInjections();
	        
	        setOpenEJBStatusAsStarted();
	    
	        
		}catch( Exception e ){
			e.printStackTrace();
		}
	}
	
	private void bindRunningApplications(){
		Server jettyServer = ServerHolder.getServer();
		
		Handler[] handlers = jettyServer.getHandlers();
		for( Handler handler : handlers ){
			if( handler instanceof WebAppContext ){
				WebAppContext webapp = (WebAppContext) handler;
											
				if( webapp.isStarted() )
					openejbBinder.start( webapp );
			}
		}			
	}	
	
	private void injectEJBInjections(){
		Server jettyServer = ServerHolder.getServer();
		Handler[] handlers = jettyServer.getHandlers();
		
		EJBInjectionManager injector = new EJBInjectionManager();
		
		for( Handler handler : handlers ){
			if( handler instanceof WebAppContext ){
				WebAppContext webapp = (WebAppContext)handler;
				if( !webapp.getContextPath().equals("/openejb") )
					injector.manage( (WebAppContext) handler );
			}
		}
	}
	
	private void setOpenEJBStatusAsStarted() throws Exception{
	    URL pingUrl = new URL("http://localhost:8080/openejbmonitor/set");
		URLConnection conn = pingUrl.openConnection();
		((HttpURLConnection)conn).getResponseMessage();
		
		conn.connect();
		InputStream read = conn.getInputStream();
		byte[] bytes = new byte[100];
		read.read( bytes );
		String answer = new String( bytes );
		
		System.out.println( answer );
	}
	
	
}
